package com.kkwrite.regimen.provider.controller.item;

import com.kkwrite.regimen.common.entity.dto.OutDTO;
import com.kkwrite.regimen.common.entity.dto.item.ItemDetailDTO;
import com.kkwrite.regimen.common.entity.dto.shop.PageableItemDTO;
import com.kkwrite.regimen.provider.exception.ServiceException;
import com.kkwrite.regimen.provider.service.item.ItemService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.kkwrite.regimen.common.constant.Constant.BOOLEAN_STRING_FALSE;
import static com.kkwrite.regimen.common.constant.Constant.BOOLEAN_STRING_TRUE;

/** 
 * 项目服务
 *
 * @author Soosky Wang
 * @date 2018年7月20日 下午10:33:37 
 * @version 1.0.0
 */
@RestController
@RequestMapping("/item")
public class ItemController {
	
	private static final Logger logger = LoggerFactory.getLogger(ItemController.class);
	
	@Resource(name = "itemService")
	private ItemService itemService;

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@GetMapping("/getitemdetail/{itemId}")
	public OutDTO getItemDetail(@PathVariable Long itemId,String city) {
		if (logger.isDebugEnabled()) {
			logger.debug("ItemController.getItemDetail(), itemId = " + itemId);
		}
		
		OutDTO outDTO = new OutDTO();
		try {
			ItemDetailDTO itemDetailDTO = itemService.getItemDetail(itemId,city);
			outDTO.setData(itemDetailDTO);
			outDTO.setSuccess("true");
			return outDTO;
		} catch (ServiceException e) {
			logger.error("ItemController.getItemDetail(), 查询项目信息出错 itemId = {} _ {}", itemId, e.getMessage());
			e.printStackTrace();
			return new OutDTO("failed", e.getMessage());
		}
	}

	@GetMapping(value = "/review/{itemId}")
	public OutDTO getItemReview(@PathVariable Integer itemId) {
		try {
			return OutDTO.of(BOOLEAN_STRING_TRUE, "OK", "OK", itemService.queryReview(itemId));
		} catch (ServiceException e) {
			return OutDTO.of(BOOLEAN_STRING_FALSE, e.getMessage(), null, null);
		} catch (Exception e) {
			return OutDTO.of(BOOLEAN_STRING_FALSE, "服务异常", null, null);
		}
	}

	@GetMapping(value = "/indexPageable/{city}")
	public OutDTO<List<ItemDetailDTO>> getIndexItemPageable(@PathVariable String city,Integer itemType, Integer sort, Integer sortType, Integer page, Integer pageSize) {
		return new OutDTO<>("true", null, null, itemService.getIndexItemPageable(city,itemType, sort, sortType, page, pageSize));
	}
	
	@GetMapping("/countshopnumbyitemid/{itemId}")
	public OutDTO countShopNumByItemId(@PathVariable Long itemId,String city) throws ServiceException {
		try {
			OutDTO outDTO = new OutDTO("true", "");
			Integer shopNum = itemService.countShopNumByItemId(itemId,city);
			Map<String, Integer> result = new HashMap<>();
			result.put("shopNum", shopNum);
			outDTO.setData(result);
			return outDTO;
		} catch (Exception e) {
			e.printStackTrace();
			return new OutDTO("false", e.getMessage());
		}
	}

	@GetMapping("/querySysItems")
	public OutDTO querySysItems(Integer page,Integer size,Integer sortType,
			HttpServletRequest request) throws ServiceException {
		try {
			OutDTO outDTO = new OutDTO("true", "");
			outDTO.setData(itemService.querySysItems(page,size,sortType));
			return outDTO;
		} catch (Exception e) {
			e.printStackTrace();
			return new OutDTO("false", e.getMessage());
		}
	}

}
